package psd.model;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import psd.config.ConfigurationReader;

import com.thoughtworks.xstream.XStream;




public class RuleSetManager {
	String ruleSetPath = "";

	public RuleSetManager(String filePath) {
		ruleSetPath = filePath;
	}

	public RuleSet ReadRules() {
		RuleSet ruleSet = null;
		try {
			File file = new File(ruleSetPath);
			FileInputStream fis = new FileInputStream(file);		
			XStream xstream = new XStream();		
			ruleSet = (RuleSet) xstream.fromXML(fis);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ruleSet;
	}
	
	public String GetFileContent()
	{
		try {			
			  BufferedReader reader = new BufferedReader( new FileReader (ruleSetPath));
			    String line  = null;
			    StringBuilder stringBuilder = new StringBuilder();
			    String ls = System.getProperty("line.separator");
			    while( ( line = reader.readLine() ) != null ) {
			        stringBuilder.append( line );
			        stringBuilder.append( ls );
			    }
			    return stringBuilder.toString();

			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	public void WriteRule(String contentFile)
	{
		FileWriter writer;
		try {
			writer = new FileWriter(ConfigurationReader.Instance().getProperty(
			"ruleSetPath"));
			writer.write(contentFile);
			writer.flush();
			writer.close();	
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	
	}

}
